package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.common.enums.QuestionEnum;
import com.example.demo.common.enums.StaticValues;
import com.example.demo.entity.Comment;
import com.example.demo.entity.Question;
import com.example.demo.entity.User;
import com.example.demo.entity.dto.QuestionDTO;
import com.example.demo.mapper.CommentMapper;
import com.example.demo.mapper.QuestionMapper;
import com.example.demo.service.ifs.ICommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.service.utils.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.swing.*;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ak
 * @since 2020-04-02
 */
@Service
@Slf4j
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {
    @Autowired
    CommentMapper commentMapper;
    @Autowired
    QuestionMapper questionMapper;
    @Autowired
    RedisTemplate<String,Object> redisTemplate;

    public List<Comment> getCommentList(Integer questionId) {
        log.info("根据问题id获取评论列表，查询数据库");
        //获取一级评论
        List<Comment> commentList = super.list(new QueryWrapper<Comment>()
                .select("*")
                .eq("c_question_id", questionId)
                .eq("c_type", 1));
        //获取二级评论
        for (Comment comment : commentList) {
            List<Comment> secondCommentList = super.list(new QueryWrapper<Comment>()
                    .select("*")
                    .eq("c_comment_id", comment.getCId())
                    .eq("c_type", 2));
            comment.setSecondCommentList(secondCommentList);
        }
        return commentList;
    }


    public void insertComment(Comment comment) {
        //格式化时间
        String formatTime = CommonUtils.getFormatTime();
        //获取用户信息
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        comment.setCCreate(formatTime);
        //TODO 登录判断
        if (user!=null){
            comment.setCAuthor(user.getUUsername());
            comment.setUser(user);
        }
        super.save(comment);
        if (comment.getCType().equals(QuestionEnum.FIRST_COMMENT.getCode())){
            //更新问题评论数量
            questionMapper.updateCommentCount(comment.getCQuestionId());
        }else if (comment.getCType().equals(QuestionEnum.SECOND_COMMENT.getCode())){
            //更新二级评论数量
            commentMapper.updateCommentCount(comment.getCCommentId());
        }
    }

    private void updateQuestionCache(Comment comment){
        Integer questionId = comment.getCQuestionId();
        String redisKey="questionList::" + questionId;
        QuestionDTO question = (QuestionDTO)redisTemplate.opsForValue().get(redisKey);
        if (question!=null){
            List<Comment> commentList = question.getCommentList();
            //更新二级评论
            if (comment.getCType().equals(QuestionEnum.SECOND_COMMENT.getCode())){
                for (Comment item : commentList) {
                    if (item.getCId().equals(comment.getCCommentId())){
                        List<Comment> secondCommentList = item.getSecondCommentList();
                        secondCommentList.add(comment);
                    }
                }
            }
            commentList.add(comment);
            redisTemplate.opsForValue().set(redisKey,question, StaticValues.QUESTION_TIME, TimeUnit.SECONDS);
        }else {
            log.error("评论出错，问题不存在!");
        }
    }
}
